package com.niiwoo.civet.trade.service.local.newRefund.notify.dubbo;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.niiwoo.activity.stage.dto.request.prize.ActivityPrizeToBackedReqDTO;
import com.niiwoo.activity.stage.enums.prize.UserPrizeUseSceneEnum;
import com.niiwoo.activity.stage.service.prize.ActivityPrizeDubboService;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.ProjectInvestorPlan;
import com.niiwoo.civet.trade.dao.mapper.ProjectInvestorPlanMapperExt;
import com.niiwoo.civet.trade.enums.ProjectSubStatusEnum;
import com.niiwoo.civet.trade.service.local.newRefund.notify.RefundNotifyHandle;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyHandleResult;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;
import com.niiwoo.tripod.provider.exception.BizException;

import lombok.extern.slf4j.Slf4j;

/**
 * 活动中心-设置红包为返现完成(借款利息折扣卡，投资加息券，发果项目逾期或者坏账，需要将剩余期数为已作废)
 * 
 * 触发条件：标的逾期
 * 
 * @author: kevin.jia
 * @version 1.0.0
 */
@Slf4j
@Component
public class ActivityPrizeBackedNotify implements RefundNotifyHandle {

	@Autowired
	private ProjectInvestorPlanMapperExt projectInvestorPlanMapperExt;
	
	@Reference(version = "1.0.0")
	private ActivityPrizeDubboService activityPrizeDubboService;
	
	@Override
	public RefundNotifyHandleResult handle(RefundNotifyDTO refundNotifyDTO) {
		RefundNotifyHandleResult refundNotifyHandleResult = null;
		try {
			ActivityPrizeToBackedReqDTO activityPrizeToBackedReqDTO = (ActivityPrizeToBackedReqDTO) bulidParam(refundNotifyDTO);
			
			log.info("项目发生逾期，调用活动中心设置红包返现完成，ProjectId：{}, 请求参数：{}", refundNotifyDTO.getProject().getProjectId(), JSONObject.toJSONString(activityPrizeToBackedReqDTO));
			activityPrizeDubboService.setPrizeToBacked(activityPrizeToBackedReqDTO);
			log.info("项目发生逾期，调用活动中心设置红包返现完成成功， ProjectId：{}", refundNotifyDTO.getProject().getProjectId());
			refundNotifyHandleResult = new RefundNotifyHandleResult();
		} catch (Exception e) {
			refundNotifyHandleResult = new RefundNotifyHandleResult(e.getMessage());
			log.error("项目发生逾期，调用活动中心设置红包返现完成异常，异常原因：{}, 请求参数：{}", e.getMessage(), JSONObject.toJSONString(refundNotifyDTO.getRefundNotifyRecord()), e);
		}
		return refundNotifyHandleResult;
	}

	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		Project project = refundNotifyDTO.getProject();
		
		// 查询所以的计划
		List<ProjectInvestorPlan> projectInvestorPlanList = Optional.ofNullable(projectInvestorPlanMapperExt.queryProjectInvestorPlanByProjectId(project.getProjectId()))
				.orElseThrow(() -> new BizException("TRD20011"));
		
		// 构建相关用户ID列表
		List<String> userIdList = Lists.newArrayList();
		projectInvestorPlanList.forEach(projectInvestorPlan -> {
			if (!userIdList.contains(projectInvestorPlan.getUserId())) {
				userIdList.add(projectInvestorPlan.getUserId());
			}
		});
		userIdList.add(project.getBorrowUserId());
		
		// 构建请求参数
		ActivityPrizeToBackedReqDTO activityPrizeToBackedReqDTO = new ActivityPrizeToBackedReqDTO();
		activityPrizeToBackedReqDTO.setProjectId(project.getProjectId());
		activityPrizeToBackedReqDTO.setUserIdList(userIdList);
		if (ProjectSubStatusEnum.REPAYING_OVERDUE.getSubStatus() == project.getSubStatus()) {
			activityPrizeToBackedReqDTO.setUserPrizeUseSceneEnum(UserPrizeUseSceneEnum.NOMAL_TO_OVERDUE);
		} else {
			activityPrizeToBackedReqDTO.setUserPrizeUseSceneEnum(UserPrizeUseSceneEnum.OVERDUE_TO_BADDEBTS);
		}
		return activityPrizeToBackedReqDTO;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.ACTIVITY_PRIZE_BACKED;
	}	
}